package com.example.designpatterns.prototype;

import java.io.*;

/**
 * 深克隆的具体原型类
 * 包含引用类型的属性，需要深克隆
 */
public class DeepConcretePrototype implements Prototype, Serializable {
    private static final long serialVersionUID = 1L;
    
    private String id;
    private String name;
    private InnerObject innerObject; // 引用类型
    
    public DeepConcretePrototype() {
    }
    
    public DeepConcretePrototype(String id, String name, InnerObject innerObject) {
        this.id = id;
        this.name = name;
        this.innerObject = innerObject;
    }
    
    /**
     * 深克隆实现 - 通过序列化和反序列化实现
     */
    @Override
    public Prototype clone() {
        try {
            // 写入字节流
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(bos);
            oos.writeObject(this);
            
            // 从字节流读取
            ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
            ObjectInputStream ois = new ObjectInputStream(bis);
            return (Prototype) ois.readObject();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    
    // Getters and Setters
    public String getId() {
        return id;
    }
    
    public void setId(String id) {
        this.id = id;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public InnerObject getInnerObject() {
        return innerObject;
    }
    
    public void setInnerObject(InnerObject innerObject) {
        this.innerObject = innerObject;
    }
    
    @Override
    public String toString() {
        return "DeepConcretePrototype{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", innerObject=" + innerObject +
                '}';
    }
} 